package com.xywl.escort.view.order;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.xywl.escort.R;
import com.xywl.escort.base.BaseActivity;
import com.xywl.escort.view.order.delivery.DeliveryOrderFragment;
import com.xywl.escort.view.order.repo.RepoOrderFragment;
import com.xywl.escort.view.order.subscribe.SubscribeOrderFragment;
import com.xywl.escort.view.order.subscription.SubscriptionOrderFragment;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.android.AndroidInjection;
import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.support.HasSupportFragmentInjector;

/**
 * 认购订单
 */

@Route(path = "/xy/order/subscription")
public class SubscriptionOrderActivity extends BaseActivity implements HasSupportFragmentInjector {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    @Inject
    DispatchingAndroidInjector<Fragment> supportFragmentInjector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase_order);
        ButterKnife.bind(this);

        showBackButton();
        setTitleText(R.string.subscription_order);

        Fragment projectFragment = SubscriptionOrderFragment.newInstance(0);
        Fragment subscribeFragment = SubscribeOrderFragment.newInstance(2);
        Fragment deliveryFragment = DeliveryOrderFragment.newInstance(3);
        Fragment repoFragment = RepoOrderFragment.newInstance(4);
        String[] tabNames = new String[]{"项目", "认购", "发货", "收购"};
        Fragment[] fragments = new Fragment[]{projectFragment, subscribeFragment, deliveryFragment, repoFragment};
        PurchasePagerAdapter adapter = new PurchasePagerAdapter(getSupportFragmentManager(), tabNames, fragments);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(tabNames.length);
        mViewPager.setCurrentItem(0);
    }

    @Override
    public AndroidInjector<Fragment> supportFragmentInjector() {
        return supportFragmentInjector;
    }

    class PurchasePagerAdapter extends FragmentPagerAdapter {
        private String[] tabNames;
        private Fragment[] mFragments;

        private PurchasePagerAdapter(FragmentManager fm, String[] tabNames, Fragment[] fragments) {
            super(fm);
            this.tabNames = tabNames;
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments[position];
        }

        @Override
        public int getCount() {
            return tabNames.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabNames[position];
        }
    }
}
